package com.hopu.aop.util;

import org.apache.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * 日志切面类
 */
@Aspect
@Component
public class LogAspect {

    //日志对象
    private Logger logger = Logger.getLogger(LogAspect.class);

    /**
     * 定义切入点
     */
    @Pointcut("execution(* com.hopu.aop.service.*Service.*(..))")
    public void pointcut(){
    }

    /**
     * 环绕通知
     * @param point
     * @return
     * @throws Throwable
     */
    @Around("pointcut()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        String name = point.getSignature().getName();
        Object[] args = point.getArgs();
        logger.debug("----------=====开始执行"+name);
        logger.debug("传入参数：");
        for(Object arg : args){
            logger.debug(arg);
        }
        long start = System.currentTimeMillis();
        //执行方法
        Object result = point.proceed();
        long end = System.currentTimeMillis();
        logger.debug("------------=====结束执行"+name+",耗时：" + (end - start));
        logger.debug("返回值:" + result);
        return result;
    }
}
